const app = getApp();
var api = require('../../../api.js');
var mta = require('../../../mta_analysis.js');

Page({
  data: {
    qrcode: '',
  },
  onLoad: function (options) {

    app.bind();
    this.getErweima();

    mta.Page.init();
  },
  onReady: function () {
  
  },
  onShow: function () {
  
  },
  onHide: function () {
    var url = app.getcurrentPage();
    wx.setStorageSync("from_url", url);
  },
  onUnload: function () {
  
  },
  onPullDownRefresh: function () {

    app.bind();
    this.getErweima();
    
    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();

    //停止下拉刷新
    wx.stopPullDownRefresh();

  },
  onReachBottom: function () {
  
  },
  onShareAppMessage: function () {
    var url = app.getcurrentPage();
    var user_id = wx.getStorageSync('user_id');
    return {
      path: url + "?user_id=" + user_id,
      title: '得道1314'
    };  
  },
  //获取二维码
  getErweima:function(){
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    wx.request({
      url: api.passport.qrcode,
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token
      },
      success: function (e) {
        console.log(e)
        console.log(e.data.data);
        that.setData({
          qrcode: e.data.data,
        })
      }
    });
  },
  toImage:function (e) {
    var img = e.currentTarget.dataset.img;
    wx.getImageInfo({
      src: img,
      success: function (res) {
        console.log(res)

        wx.saveImageToPhotosAlbum({
          filePath: res.path ,
          success: function (e) {
            console.log("成功");
            wx.showToast({
              title: '二维码保存成功！',
              icon: 'none',
            })
          },
          fail: function (e) {
            wx.showToast({
              title: '二维码保存失败！',
              icon: 'none',
            })
          }
        }) 
               
      }
    })
  },
})